import Banner from "../../Banner";
import { Constant } from "../Const/Constant";
import AudioManager from "../Managers/AudioManager";
import { UIManager } from "../Managers/UIManager";
import NodeUtil from "../Utils/NodeUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SelectMapPanel extends cc.Component {
    MapButton_0: cc.Node = null;
    MapButton_1: cc.Node = null;
    MapButton_2: cc.Node = null;
    MapButton_3: cc.Node = null;
    MapButton_4: cc.Node = null;
    StartPanel: cc.Node = null;

    index: number = 0;

    protected onLoad(): void {
        this.MapButton_0 = NodeUtil.GetNode("MapButton_0", this.node);
        this.MapButton_1 = NodeUtil.GetNode("MapButton_1", this.node);
        this.MapButton_2 = NodeUtil.GetNode("MapButton_2", this.node);
        this.MapButton_3 = NodeUtil.GetNode("MapButton_3", this.node);
        this.MapButton_4 = NodeUtil.GetNode("MapButton_4", this.node);
        this.StartPanel = NodeUtil.GetNode("StartPanel", this.node);
    }

    Show(cb: Function = null) {
        this.RefreshButtons();
    }

    RefreshButtons(index: number = -1) {
        this.MapButton_0.getChildByName("Selected").active = index == 0;
        this.MapButton_0.getChildByName("Title").getChildByName("Selected").active = index == 0;
        this.MapButton_1.getChildByName("Selected").active = index == 1;
        this.MapButton_1.getChildByName("Title").getChildByName("Selected").active = index == 1;
        this.MapButton_2.getChildByName("Selected").active = index == 2;
        this.MapButton_2.getChildByName("Title").getChildByName("Selected").active = index == 2;
        this.MapButton_3.getChildByName("Selected").active = index == 3;
        this.MapButton_3.getChildByName("Title").getChildByName("Selected").active = index == 3;
        this.MapButton_4.getChildByName("Selected").active = index == 4;
        this.MapButton_4.getChildByName("Title").getChildByName("Selected").active = index == 4;

        this.StartPanel.active = index != -1;

        if (index != -1) {
            this.index = index;
            this.StartPanel.getChildByName("Icons").children.forEach(e => e.active = e.name == `${index}`);
        }
    }

    OnButtonClick(event: cc.Event) {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);

        switch (event.target.name) {
            case "MapButton_0":
                this.RefreshButtons(0);
                break;
            case "MapButton_1":
                this.RefreshButtons(1);
                break;
            case "MapButton_2":
                this.RefreshButtons(2);
                break;
            case "MapButton_3":
                this.RefreshButtons(3);
                break;
            case "MapButton_4":
                this.RefreshButtons(4);
                break;
            case "StartButton":
                UIManager.Instance.ShowPanel(Constant.Panel.LoadingPanel, [`Lv_${this.index}`]);
                break;
            case "ReturnButton":
                UIManager.Instance.HidePanel(Constant.Panel.SelectMapPanel);
                break;
        }
    }
}
